<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Api_V2_Distributor
{

    //检查分销商是否有效
    public static function check_distribution($userid){
        $info = DB::select('isfrozen', 'fxcode')->from('fenxiao')->where('memberid', '=', $userid)->and_where('status','=','1')->and_where('isfrozen','=','0')->execute()->current();
        return $info;
    }

    //获取下级会员数
    public static function get_member_num($userid,$is_member=0)
    {
        $level_num = Model_Fenxiao_Config::get_val('cfg_fenxiao_level_num');
        $fenxiao_level_num = $level_num == 2 ? 2 : 3;
        $sub_mids = Model_Fenxiao::get_sub_mids($userid,$fenxiao_level_num);
        $num=0;
        if(empty($sub_mids))
        {
            return 0;
        }
        if($is_member==0)
        {
            $num = DB::query(Database::SELECT,"select count(*) as num from sline_fenxiao where memberid in (".implode(',',$sub_mids).") and status=1")->execute()->get('num');
        }
        else
        {
            $sub_mids = Model_Fenxiao::get_sub_mids($userid,1);
            $num = DB::query(Database::SELECT,"select count(*) as num from sline_fenxiao where memberid in (".implode(',',$sub_mids).") and status=-1")->execute()->get('num');
        }
        return $num;
    }

    /**
     * 获取分销客户
     * @param $params
     * @return mixed
     */
    public static function get_submembers($params)
    {
        $userid = $params['userid'];
        $fxcode = $params['fxcode'];
        $keyword = $params['keyword'];
        $page = intval($params['page']);
        $page = $page<1?1:$page;
        $pagesize = $params['pagesize'] ? $params['pagesize'] : 10;
        $offset = ($page-1)*$pagesize;
        $w = " where a.fxpcode='".$fxcode."' and a.status='-1' ";
        if(!empty($keyword))
        {
            $w.= " and (c.nickname like '%{$keyword}%' or c.mobile like '%{$keyword}%' or c.email like '%{$keyword}%') ";
        }
        $order_by = " order by a.fxjointime desc ";
        $sql = "select a.memberid,a.fxjointime,a.fxrankid,b.total from sline_fenxiao a inner join sline_member c on a.memberid=c.mid
        left join (select sum(amount) as total,fxmemberid from sline_fenxiao_record where memberid=".$userid." and type=0 group by fxmemberid) b
        on a.memberid=b.fxmemberid {$w} {$order_by} limit {$offset},{$pagesize}";

        $totalSql = "select count(*) as num from sline_fenxiao a inner join sline_member c on a.memberid=c.mid
        left join (select sum(amount) as total,fxmemberid from sline_fenxiao_record where memberid=".$userid." and type=0 group by fxmemberid) b
        on a.memberid=b.fxmemberid {$w}";
        $totalNum = DB::query(Database::SELECT,$totalSql)->execute()->get('num');

        $list = DB::query(Database::SELECT,$sql)->execute()->as_array();
        foreach($list as &$row)
        {
            $member_info = Model_Member::get_member_byid($row['memberid']);
            $row['nickname'] = $member_info['nickname'];
            $row['litpic'] = Model_Api_V2_Common::get_member_header_pic($member_info['litpic']);
            $row['account'] = empty($member_info['mobile'])?$member_info['email']:$member_info['mobile'];
        }
        return array('list'=> $list, 'total'=> $totalNum);
    }

    /**
     * 获取我的下级分销商列表
     * @return mixed
     */
    public static function get_subfx($params)
    {
        $group = $params['group'];
        $userid = $params['userid'];
        $order = $params['order'];
        $keyword = $params['keyword'];

        $page = intval($params['page']);
        $page = $page<1?1:$page;
        $pagesize = 10;
        $offset = ($page-1)*$pagesize;

        $level_num = Model_Fenxiao_Config::get_val('cfg_fenxiao_level_num');
        $fenxiao_level_num = $level_num == 2 ? 2 : 3;

        //$sub_mids = Model_Fenxiao::get_sub_mids($userid,3);
        $sub_mids = Model_Fenxiao::get_sub_mids($userid,$fenxiao_level_num);
        $first_sub_mids = Model_Fenxiao::get_sub_mids($userid, 1);
        if($group)
        {
            $sub_mids = Model_Fenxiao::get_sub_mids($userid, $group,true);
        }

        if(empty($sub_mids))
        {
            return array();
        }

        $order_by = "";
        switch($order)
        {
            case 1:
                $order_by = " order by ifnull(b.total,0) desc ";
                break;
            case 2:
                $order_by = " order by ifnull(b.total,0) asc ";
                break;
            case 3:
                $order_by = " order by a.fxjointime desc ";
                break;
            case 4:
                $order_by = " order by a.fxrankid desc ";
                break;
        }

        $w  = " where a.status=1 and a.memberid in (".implode(',',$sub_mids).")";
        if(!empty($keyword))
        {
            $w.= " and (c.nickname like '%{$keyword}%' or c.mobile like '%{$keyword}%' or c.email like '%{$keyword}%') ";
        }

        $sql = "select a.memberid,a.fxjointime,a.fxrankid,b.total from sline_fenxiao a
inner join sline_member c on a.memberid=c.mid
left join (select sum(amount) as total,fxmemberid from sline_fenxiao_record where memberid=".$userid." and type=0 group by fxmemberid) b on a.memberid=b.fxmemberid {$w} {$order_by} limit {$offset},{$pagesize}";

        $list = DB::query(Database::SELECT,$sql)->execute()->as_array();
        foreach($list as &$row)
        {
            $member_info = Model_Member::get_member_byid($row['memberid']);
            $row['nickname'] = $member_info['nickname'];
            $row['litpic'] = Model_Api_V2_Common::get_member_header_pic($member_info['litpic']);
            $row['fxrank_name'] = Model_Fenxiao_Rank::get_name($row['fxrankid']);
            $row['sub_num'] = count(Model_Fenxiao::get_sub_mids($row['memberid'],1));
            $row['fxjointime'] = date('Y-m-d',$row['fxjointime']);
            $row['account'] = empty($member_info['mobile'])?$member_info['email']:$member_info['mobile'];
            $row['sub_level'] = Model_Fenxiao::get_rank($userid,$row['memberid']);
            $row['total'] = empty($row['total'])?0:$row['total'];
            $row['total'] = round($row['total'],2);

        }
        return $list;
    }
    public static  function get_products2($params)
    {
        $typeid = intval($params['typeid']);
        $userid = intval($params['userid']);
        $keyword = $params['keyword'];
        $ordertype = $params['ordertype'];

        //分销分组ID
        $fxgroupid = DB::select('fxgroupid')->from('fenxiao')->where("memberid",'=',$userid)->execute()->get('fxgroupid');

        $list = self::search_products2(array('typeid'=>$typeid,'ordertype'=>$ordertype,'groupid'=>$fxgroupid),$keyword);
        $list_new = array();

        foreach($list as $key=>$v) {
            $commission_type=Model_Fenxiao_Config::get_val('cfg_fenxiao_commission_type_'.$v['typeid'],$fxgroupid);
            $v['commission_type'] = $commission_type;
            $min_price = self::get_price($v['typeid'],$v['tid']);
            $v['price'] = empty($v['price'])?0:$v['price'];
            $min_price = is_array($min_price)?$min_price['price']:$min_price;
            $min_price = empty($min_price)?0:$min_price;
            $v['price'] = !empty($min_price)?$min_price:$v['price'];

            $v['id'] = $v['tid'];

            list($profit,$suitid)=self::get_profit_suitid($typeid,$v);
            $v['profit'] = $profit;
            $v['suitid'] = $suitid;

            //收益
            $v['profit'] = self::get_prospect_fenxiao_price($typeid,$v,$fxgroupid);

            $profit = round($v['profit'],2);
            if(empty($profit)) {
                continue;
            }else{
                $v['typeid'] = $typeid;
                $module_info = $row = ORM::factory('model',$v['typeid'])->as_array();
                $v['label'] = $module_info['modulename'];//标签
                $v['producttitle'] = $v['title'];//标题
                $v['series'] = Product::product_number($v['tid'], $typeid);//编号
                $v['litpic'] = Model_Api_V2_System::uniapp_img($v['litpic']);
                $list_new[]=$v;
            }

        }
        //按照佣金升序排列
        if($ordertype==3)
        {
            $list_new=self::assortArray2($list_new,'SORT_ASC','profit');
        }
        //按照佣金降序排列
        if($ordertype==4)
        {
            $list_new=self::assortArray2($list_new,'SORT_DESC','profit');
        }
        return $list_new;
    }
    //获取产品，必须要有tid 和 typeid
    public static function search_products2($params,$keyword)
    {
        $value_arr = array();
        //编号查询
        $where = " where a.ishidden=0 ";

        if(is_numeric($keyword))
        {
            $result = St_Product::product_series($keyword,'',true);
            $where .= " AND a.id='{$result['id']}'";
        }
        else if(!empty($keyword))
        {
            //关键字查询
            $where .= " AND a.title like :keyword ";
            $value_arr[':keyword'] = '%'.$keyword.'%';
        }

        $typeid = $params['typeid'];
        $table = self::get_table($typeid);

        if(empty($table))
        {
            return array(
                'total' => 0,
                'list' => null
            );
        }

        if($table == 'model_archive')
        {
            $where.=" AND a.typeid={$typeid} ";
        }

        $order = " order by ifnull(b.displayorder,999999) asc,b.cash1 desc ";
        switch ($params['ordertype'])
        {
            case 1://综合排序
                $order = " order by ifnull(b.displayorder,999999) asc,b.cash1 desc";
                break;
            case 2://热度排序
                $order = " order by a.bookcount desc,b.cash1 desc";
                break;
            case 5://最新商品
                $order = " order by a.addtime desc,b.cash1 desc";
                break;
        }
        if($typeid==13){
            $bookcount = 'a.virtualnum as bookcount';
        }else{
            $bookcount ='a.bookcount' ;
        }
        $sql = " select a.title,a.supplierlist,a.litpic,a.webid,a.satisfyscore,a.id as tid,a.aid,a.modtime,a.price,{$bookcount},a.addtime,b.* from sline_{$table} a left join sline_fenxiao_ratio  b on a.id=b.productid and b.typeid={$typeid} and b.groupid={$params['groupid']} {$where}  and a.price>0 {$order} ";
        $sql.= "LIMIT 0,40";
        $arr = DB::query(1,$sql)->parameters($value_arr)->execute()->as_array();

        return $arr;
    }

    //获取某个产品的最低价
    public static  function get_price($typeid,$productid)
    {
        $model_info = DB::select()->from('model')->where('id','=',$typeid)->execute()->current();

        $maintable = $model_info['maintable'];
        $letter_arr = explode('_',$maintable);
        foreach($letter_arr as &$letter)
        {
            $letter = ucfirst($letter);
        }
        $class_name='Model_'.implode('_',$letter_arr);
        $price = 0;
        if(class_exists($class_name)&& method_exists($class_name,'get_minprice') )
        {
            $price = call_user_func(array($class_name, 'get_minprice'),$productid);
        }
        return $price;
    }
    //获取表名
    public static  function get_table($typeid)
    {
        $model=ORM::factory('model',$typeid);
        if(!$model->loaded())
            return false;
        return $model->maintable;
    }

    //获取分销订单
    public static function get_orders($params)
    {
        $rank = intval($params['rank']);
        $typeid = intval($params['type_id']);
        $status = $params['status'];
        $userid = $params['userid'];
        $order = $params['order'];
        $keyword = $params['keyword'];
        $page = intval($params['page']);
        $pagesize = intval($params['pagesize']);
        $page = $page<1?1:$page;
        $pagesize = $pagesize < 1 ? 10 : $pagesize ;
        $offset = ($page-1)*$pagesize;

        $level_num = Model_Fenxiao_Config::get_val('cfg_fenxiao_level_num');
        $level_num = $level_num == 2 ? 2 : 3;
        $sub_mids = Model_Fenxiao::get_sub_mids($userid,$level_num);
        if(!empty($rank))
        {
            // 自购订单
            if($rank == '4')
            {
                $sub_mids = array($userid);
            }
            else
            {
                $sub_mids = Model_Fenxiao::get_sub_mids($userid,$rank,true);
            }
        }
        else
        {
            $sub_mids[] = $userid;
        }

        if(!empty($sub_mids) && !empty($keyword))
        {
            $keyword_submids = Model_Fenxiao::get_keyword_sub_mids($keyword);
            $keyword_w = !empty($keyword_submids)? " or mid in (".implode(',',$keyword_submids).")":'';

            $sub_members = DB::query(Database::SELECT,"select mid from sline_member where mid in(".implode(',',$sub_mids).") and (mobile like '%{$keyword}%' or email like '%{$keyword}%' or nickname like '%{$keyword}%' {$keyword_w})")->execute()->as_array();
            $sub_mids = array();
            foreach($sub_members as $sub_member)
            {
                if(!empty($sub_member['mid']))
                {
                    $sub_mids[] = $sub_member['mid'];
                }
            }
        }

        if(empty($sub_mids))
        {
            return array('list'=> array(), 'total'=> 0);
        }

        $w = " where a.memberid in (".implode(',',$sub_mids).") ";
        if(!empty($typeid))
        {
            $w.=" and a.typeid={$typeid} ";
        }

        if(!empty($status) || $status===0 || $status==='0')
        {
            $w.=" and a.status='{$status}' ";
        }

        $order_by = "";
        switch($order)
        {
            case 2:
                $order_by = " order by ifnull(b.amount,0) desc,a.addtime desc ";
                break;
            case 3:
                $order_by = " order by ifnull(b.amount,0) asc,a.addtime desc ";
                break;
            default:
                $order_by = " order by a.addtime desc";
        }
        $sql = "select a.*,b.amount as fenxiao_amount from sline_member_order a left join sline_fenxiao_record b on a.id=b.orderid and (b.memberid is null or b.memberid=".$userid.") {$w} {$order_by} limit {$offset},{$pagesize}";
        $list = DB::query(Database::SELECT,$sql)->execute()->as_array();
        // 总条数
        $totalSql = "select count(*) as num from sline_member_order a left join sline_fenxiao_record b on a.id=b.orderid and (b.memberid is null or b.memberid=".$userid.") {$w}";
        $total = DB::query(Database::SELECT,$totalSql)->execute()->get('num');
        foreach ($list as &$v)
        {
            $model = ORM::factory('model', $v['typeid']);
            $table = $model->maintable;
            if ($table)
            {
                $py = empty($model->correct) ? $model->pinyin : $model->correct;
                $info = ORM::factory($table, $v['productautoid'])->as_array();
                $out = $info;
                $v['litpic'] = Model_Api_V2_System::uniapp_img($out['litpic']);
                $v['url'] = St_Functions::get_web_url($info['webid']) . "/{$py}/show_{$info['aid']}.html";
            }
            $v['totalprice'] = Model_Member_Order::order_total_price($v['id'],$v);

            $commission_type=Model_Fenxiao_Config::get_val('cfg_fenxiao_commission_type_'.$v['typeid'],0);
            $rank = Model_Fenxiao::get_rank($userid,$v['memberid']);
            $rank_index=$rank;
            $v['commission_type'] = $commission_type;
            $order_temp = Model_Member_Order::order_info($v['ordersn']);
            $commission_info = Model_Fenxiao::get_order_fenxiao_info($order_temp);

            if(empty($rank_index))
            {
                $v['commission_amount'] = round($commission_info['commission'],2);
            }
            else
            {
                $v['commission_amount'] = round($commission_info['commission'.$rank_index],2);
            }

            $member_info = Model_Member::get_member_byid($v['memberid']);
            $v['member'] = $member_info;

            $parent_fenxiao = Model_Fenxiao::get_ancestor_fenxiao($v['memberid'],1);
            $v['parent_member'] = Model_Member::get_member_byid($parent_fenxiao['memberid']);

            $v['addtime'] = date('Y-m-d H:i',$v['addtime']);
            $v['fenxiao_amount'] = empty($v['fenxiao_amount'])?0:$v['fenxiao_amount'];
            $v['status_name'] = Model_Member_Order::get_status_name($v['status']);
        }

        return array('list'=> $list, 'total'=> $total);
    }

    //获取返佣总和
    public static  function get_total_profit($userid)
    {
        $result = DB::query(Database::SELECT,"select amount from sline_fenxiao_record where memberid='{$userid}' and type=0")->execute()->as_array();
        $amount = 0;
        foreach($result as $row)
        {
            $amount+=$row['amount'];
        }
        return $amount;
    }

    //获取已付款，但未返佣的佣金总和
    public static function get_total_unfinish_profit($userid)
    {
        $sql = "SELECT a.* FROM sline_fenxiao_data AS a ";
        $sql.= "LEFT JOIN sline_member_order AS b ON(a.order_id=b.id) ";
        $sql.= "WHERE (a.memberid={$userid} OR a.memberid1={$userid} OR a.memberid2={$userid} OR a.memberid3={$userid}) ";
        $sql.= "AND b.status=2 ";
        $order = DB::query(1, $sql)->execute()->as_array();
        if(count($order)<1){
            return 0;
        }
        $info = array(
            0 => array(),//自购订单
            1 => array(),//下1级分销订单
            2 => array(),//下2级分销订单
            3 => array(),//下3级分销订单
        );

        foreach ($order as $v){
            if($v['memberid']==$userid){
                array_push($info[0],$v['order_id']);
            }
            if($v['memberid1']==$userid){
                array_push($info[1],$v['order_id']);
            }
            if($v['memberid2']==$userid){
                array_push($info[2],$v['order_id']);
            }
            if($v['memberid3']==$userid){
                array_push($info[3],$v['order_id']);
            }
        }
        $total = 0;
        foreach ($info as $k=>$v){
            if(count($v)>0){
                $order_id = implode(',',$v);
                $sum_sql = $k<1?"sum(fx_self) as price":"sum(fx_top{$k}) as price";
                $price = DB::select(DB::expr($sum_sql))->from('member_order_compute')->where("order_id IN({$order_id})")->execute()->get('price');
                $total = bcadd($total,$price,2);
            }
        }
        return $total;
    }

    //和Controller_Mobile_Fenxiao_Jieshao的函数一致
    public static function get_prospect_fenxiao_price($typeid,$product,$fxgroupid)
    {
        // $member_info = Model_Member_Login::check_login_info();
        // $fenxiao = DB::select()->from('fenxiao')->where('memberid','=',$member_info['mid'])->execute()->current();

        $commission_type=Model_Fenxiao_Config::get_val('cfg_fenxiao_commission_type_'.$typeid,$fxgroupid);
        $min_price = self::get_price($typeid,$product['id']);

        $product['price'] = empty($product['price'])?0:$product['price'];
        $min_price = is_array($min_price)?$min_price['price']:$min_price;
        $min_price = empty($min_price)?0:$min_price;
        $product['price'] = !empty($min_price)?$min_price:$product['price'];

        if($commission_type==1)
        {
            $commission_rate = Model_Fenxiao::get_commission_rate($typeid,$product['id'],$fxgroupid);
            $profit =number_format($product['price']*$commission_rate[0],2);
        }
        else if($commission_type==2)
        {
            $commission_rate = Model_Fenxiao::get_commission_rate_platform($typeid,$product['id'],$fxgroupid);


            $temp_order = array('typeid'=>$typeid,'productautoid'=>$product['id'],'totalprice'=>$product['price'],'dingnum'=>1,'suitid'=>$product['suitid'],'channel_id'=>1);

            if($typeid==5)
            {

                $platform_price = Model_Member_Order_Compute::caculate_platform_commission($temp_order);
            }
            else {
                if (empty($product['supplierlist'])) {
                    $platform_price = $product['price'];
                } else {
                    $platform_price = Model_Member_Order_Compute::caculate_platform_commission($temp_order);
                    if($platform_price['settlement_type']==2){//销售价
                        if ($platform_price['commission_type'] == 1) {
                            $platform_price = $product['price'] * $platform_price['commission_ratio'];
                        } else {
                            $platform_price = $platform_price['commission_ratio'];
                        }
                    }else{//结算价
                        $platform_price = $product['profit'];
                    }
                }
            }
            $profit = number_format($platform_price*$commission_rate[0],2);
        }
        else
        {
            $commission_cash = Model_Fenxiao::get_commission_cash_line($typeid,$product['id'],$fxgroupid);
            if($typeid==1){
                $time = strtotime(date('Y-m-d',time()));
                //取该产品的今天有报价的那1个套餐
                $sql_suit="select propgroup from sline_line_suit_price where day>=".$time.' and lineid = '.$product['id'].' order by suitid asc';
                $suit_data = DB::query(1,$sql_suit)->execute()->current();
                //2:成人 1:小孩 3:老人,4:人群四，5：人群五，6：人群6
                //假如报了儿童，就在佣金设置里面找儿童的收益
                $people_relationship = [
                    1 => 'child',
                    2 => 'adult',
                    3 => 'old',
                    4 => 'people4',
                    5 => 'people5',
                    6 => 'people6',
                ];
                $_profit = [];
                //如果有报价
                if($suit_data['propgroup']){
                    $propgroup = explode(',',$suit_data['propgroup']);//该套餐的报价人群
                    foreach ($propgroup as $propgroup_v) {
                        if(isset($people_relationship[$propgroup_v])){
                            $_people = $people_relationship[$propgroup_v];//报价人群
                            $_profit[] = $commission_cash[0][$_people];
                        }
                    }
                }
                $profit = number_format(max($_profit),2);//金额最大的
            }else{
                $profit =number_format($commission_cash[0]['adult'],2);
            }
        }
        return $profit;
    }

    // 检查会员是否是分销商
    public static function check_member_distribution($userid)
    {
        $result = false;
        if (St_Functions::is_normal_app_install('mobiledistribution')) {
            $result = DB::select('id', 'status','isfrozen','fxcode')->from('fenxiao')->where('memberid', '=', $userid)->execute()->current();
        }

        if (!empty($result)) {
            $result['isfenxiao'] = $result['status'] == 1 && $result['isfrozen'] == 0 ? true : false;
            switch ($result['status']) {
                case -1:
                    $result = false;
                    break;
                case 0:
                    $result['statusname'] = '等待审核';
                    break;
                case 1:
                    $result['statusname'] = '审核通过';
                    break;
                case 2:
                    $result['statusname'] = '审核不通过';
                    break;
            }
        }
        else
        {
            $result = false;
        }

        return $result;
    }

    public static function assortArray2($array,$flag,$keyword)
    {
        $sort = array(
            'direction' => $flag, //排序顺序标志 1 ，SORT_DESC 降序；2 ，SORT_ASC 升序
            'field' => $keyword,       //排序字段
        );
        $arrSort = array();
        foreach ($array AS $uniqid => $row) {
            foreach ($row AS $key => $value) {
                $arrSort[$key][$uniqid] = $value;
            }
        }
        if ($sort['direction']) {
            array_multisort($arrSort[$sort['field']], constant($sort['direction']), $array);
        }
        return $array;
    }

    public static function get_profit_suitid($typeid,$v){
        switch ($typeid) {
            case 1:
                $suitid = DB::select('suitid')
                    ->from('line_suit_price')
                    ->where('lineid','=',$v['id'])
                    ->and_where('adultprice','>',0)
                    ->and_where('day','>',strtotime(date('Y-m-d',time())))
                    ->execute()
                    ->get('suitid');
                $profit=Model_Line::get_profit($v['id']);
                break;
            case 2:
                $suitid = DB::select('suitid')
                    ->from('hotel_room_price')
                    ->where('hotelid','=',$v['id'])
                    ->and_where('price','>',0)
                    ->and_where('day','>',strtotime(date('Y-m-d',time())))
                    ->execute()
                    ->get('suitid');
                $profit=Model_Hotel::get_profit($v['id']);
                break;
            case 3:
                $suitid = DB::select('suitid')
                    ->from('car_suit_price')
                    ->where('carid','=',$v['id'])
                    ->and_where('adultprice','>',0)
                    ->and_where('day','>',strtotime(date('Y-m-d',time())))
                    ->execute()
                    ->get('suitid');
                $profit=Model_Car::get_profit($v['id']);
                break;
            case 5:
                $suitid = DB::select('ticketid')
                    ->from('spot_ticket_price')
                    ->where('spotid','=',$v['id'])
                    ->and_where('adultprice','>',0)
                    ->and_where('day','>',strtotime(date('Y-m-d',time())))
                    ->execute()
                    ->get('ticketid');
                $profit=Model_Spot::get_profit($v['id']);
                break;
            case 13:
                $suitid = 0;
                $profit=Model_Tuan::get_profit($v['id']);
                break;
            case 21:
                $suitid = DB::select('id')
                    ->from('malls_suit')
                    ->where('productid','=',$v['id'])
                    ->and_where('price','>',0)
                    ->execute()
                    ->get('id');
                $profit=Model_Malls::get_profit($v['id']);
                break;
            case 114:
                $suitid = DB::select('suitid')
                    ->from('outdoor_suit_price')
                    ->where('spotid','=',$v['id'])
                    ->and_where('price','>',0)
                    ->and_where('day','>',strtotime(date('Y-m-d',time())))
                    ->execute()
                    ->get('suitid');
                $profit=Model_Outdoor::get_profit($v['id']);
                break;
            case 118:
                $suitid = DB::select('id')
                    ->from('techan_suit')
                    ->where('productid','=',$v['id'])
                    ->and_where('price','>',0)
                    ->execute()
                    ->get('id');
                $profit=Model_Techan::get_profit($v['id']);
                break;
            case 121:
                $suitid = DB::select('id')
                    ->from('food_suit')
                    ->where('productid','=',$v['id'])
                    ->and_where('price','>',0)
                    ->execute()
                    ->get('id');
                $profit=Model_Food::get_profit($v['id']);
                break;
            default:
                $suitid = DB::select('suitid')
                    ->from('model_suit_price')
                    ->where('productid','=',$v['id'])
                    ->and_where('price','>',0)
                    ->and_where('day','>',strtotime(date('Y-m-d',time())))
                    ->execute()
                    ->get('suitid');
                $profit=DB::select('profit')
                    ->from('model_suit_price')
                    ->where('productid','=',$v['id'])
                    ->and_where('profit','>',0)
                    ->and_where('day','>',strtotime(date('Y-m-d',time())))
                    ->execute()
                    ->get('profit');
                break;
        }
        return [$profit,$suitid];
    }
}